package com.ruoyi.web.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.SuperCouponReceive;
import com.ruoyi.system.service.ISuperCouponReceiveService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 用户优惠券记录:记录优惠券发放/领取记录 Controller
 * 
 * @author ruoyi
 * @date 2025-08-13
 */
@RestController
@RequestMapping("/coupon/receive")
public class SuperCouponReceiveController extends BaseController
{
    @Autowired
    private ISuperCouponReceiveService superCouponReceiveService;

    @PreAuthorize("@ss.hasPermi('coupon:receive:list')")
    @GetMapping("/list")
    public TableDataInfo list(SuperCouponReceive superCouponReceive)
    {
        startPage();
        List<SuperCouponReceive> list = superCouponReceiveService.selectSuperCouponReceiveList(superCouponReceive);
        return getDataTable(list);
    }

    /**
     * 统计用户可用优惠券数量（未使用、未删除、优惠券未删除）
     */
    @PreAuthorize("@ss.hasPermi('coupon:receive:query')")
    @GetMapping("/user/{userId}/availableCount")
    public AjaxResult countUserAvailableCoupons(@PathVariable("userId") Long userId)
    {
        int count = superCouponReceiveService.countUserAvailableCoupons(userId);
        return success(count);
    }

    @PreAuthorize("@ss.hasPermi('coupon:receive:export')")
    @Log(title = "用户优惠券记录:记录优惠券发放/领取记录 ", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SuperCouponReceive superCouponReceive)
    {
        List<SuperCouponReceive> list = superCouponReceiveService.selectSuperCouponReceiveList(superCouponReceive);
        ExcelUtil<SuperCouponReceive> util = new ExcelUtil<SuperCouponReceive>(SuperCouponReceive.class);
        util.exportExcel(response, list, "用户优惠券记录:记录优惠券发放/领取记录数据");
    }

    @PreAuthorize("@ss.hasPermi('coupon:receive:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(superCouponReceiveService.selectSuperCouponReceiveById(id));
    }

    @PreAuthorize("@ss.hasPermi('coupon:receive:add')")
    @Log(title = "用户优惠券记录:记录优惠券发放/领取记录 ", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SuperCouponReceive superCouponReceive)
    {
        return toAjax(superCouponReceiveService.insertSuperCouponReceive(superCouponReceive));
    }

    @PreAuthorize("@ss.hasPermi('coupon:receive:edit')")
    @Log(title = "用户优惠券记录:记录优惠券发放/领取记录 ", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SuperCouponReceive superCouponReceive)
    {
        return toAjax(superCouponReceiveService.updateSuperCouponReceive(superCouponReceive));
    }

    @PreAuthorize("@ss.hasPermi('coupon:receive:remove')")
    @Log(title = "用户优惠券记录:记录优惠券发放/领取记录 ", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(superCouponReceiveService.deleteSuperCouponReceiveByIds(ids));
    }
}
